package com.heima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.reggie.bean.*;
import com.heima.reggie.dto.SetmealDto;
import com.heima.reggie.exception.CustomException;
import com.heima.reggie.mapper.SetmealMapper;
import com.heima.reggie.service.CategoryService;
import com.heima.reggie.service.SetmealDishService;
import com.heima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper mapper;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private SetmealDishService setmealDishService;
    /**
      * 根据分类id统计该分类下的套餐数量
      * @param
      * @param
      * @return
      */
    @Override
    public long findCpuntByCid(long cid) {
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Setmeal::getCategoryId, cid);
        return mapper.selectCount(lqw);
    }
/**
  * 分页查询
  * @param
  * @param
  * @return
  */
    @Override
    public IPage<SetmealDto> selectByPage(PageParam pageParam) {
        IPage<Setmeal> setmealDtoPage = new Page<>(pageParam.getPage(), pageParam.getPageSize());
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.like(pageParam.getName() != null, Setmeal::getName, pageParam.getName());
        IPage<Setmeal> setmealIPage = mapper.selectPage(setmealDtoPage, lqw);
        //stream流写法
        List<SetmealDto> setmealDtoList = setmealIPage.getRecords().stream().map((setmeal) -> {
            SetmealDto setmealDto = new SetmealDto();
            setmealDto.setCategoryName(categoryService.selectById(setmeal.getCategoryId()).getName());
            setmealDto.setSetmealDishes(setmealDishService.selectBySid(setmeal.getId()));
            BeanUtils.copyProperties(setmeal, setmealDto);
            return setmealDto;
        }).collect(Collectors.toList());
       /* List<Setmeal> records = setmealIPage.getRecords();
        List<SetmealDto> setmealDtoList = new ArrayList<>();
        for (Setmeal record : records) {
            Category category = categoryService.selectById(record.getCategoryId());
            SetmealDto setmealDto = new SetmealDto();
            setmealDto.setCategoryName(category.getName());
            setmealDto.setSetmealDishes(setmealDishService.selectBySid(record.getId()));
            BeanUtils.copyProperties(record,setmealDto);
            setmealDtoList.add(setmealDto);

        }*/
        IPage<SetmealDto> setmealDtoIPage = new Page<>();
        setmealDtoIPage.setRecords(setmealDtoList);
        setmealDtoIPage.setTotal(setmealIPage.getTotal());
        return setmealDtoIPage;
    }
/**
  * 新增
  * @param
  * @param
  * @return
  */
    @Override
    @CacheEvict(value = "setmealCache",allEntries = true)
    public int add(SetmealDto setmealDto) {
        int insert = mapper.insert(setmealDto);
        if (insert > 0&&setmealDto.getSetmealDishes()!=null&&setmealDto.getSetmealDishes().size()>0) {
            int row=0;
            for (SetmealDish setmealDish : setmealDto.getSetmealDishes()) {
                setmealDish.setSetmealId(setmealDto.getId());
                row+= setmealDishService.add(setmealDish);
            }
            return row==setmealDto.getSetmealDishes().size()?2:0;
        }
        return insert;
    }
/**
  * 根据ids删除
  * @param
  * @param
  * @return
  */
    @Override
    @CacheEvict(value = "setmealCache",allEntries = true)
    public int deletetByIds(List<Long> ids) {
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.in(Setmeal::getId, ids);
        lqw.eq(Setmeal::getStatus, 1);
        int total = mapper.selectCount(lqw);
        if (total>0){
            throw new CustomException("菜单处于起售状态，禁止删除！");
        }
        int i = mapper.deleteBatchIds(ids);
        int row1=0;
        int row2=0;
        if (i > 0) {
            for (Long id : ids) {
                row2 += setmealDishService.countBySid(id);
                row1 += setmealDishService.deleteBySid(id);
            }
            return row1==row2?2:0;
        }
        return i;
    }
/**
  * 更新状态
  * @param
  * @param
  * @return
  */
    @Override
    public int updateStatus(DishSi dishSi) {
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(dishSi.getStatus());
        int row = 0;
        for (Long aLong : dishSi.getId()) {
            setmeal.setId(aLong);
            row+= mapper.updateById(setmeal);
        }

        return row==dishSi.getId().size()?1:0;
    }
/**
  * 更新
  * @param
  * @param
  * @return
  */
    @Override
    @CacheEvict(value = "setmealCache",allEntries = true)
    public int update(SetmealDto setmealDto) {
        int i = mapper.updateById(setmealDto);
        if (i > 0) {
            int row1= setmealDishService.countBySid(setmealDto.getId());
            int row2= setmealDishService.deleteBySid(setmealDto.getId());
            int row3=0;
            if (row1 == row2 && setmealDto.getSetmealDishes() != null && setmealDto.getSetmealDishes().size() > 0) {
                    for (SetmealDish setmealDish : setmealDto.getSetmealDishes()) {
                        setmealDish.setSetmealId(setmealDto.getId());
                        row3+= setmealDishService.add(setmealDish);
                    }
                    return row3==setmealDto.getSetmealDishes().size()?3:0;

            }
             return row1==row2?2:0;
        }
        return i;
    }
/**
  * 根据分类id查询套餐
  * @param
  * @param
  * @return
  */
    @Override
    @Cacheable(value = "setmealCache",key="#setmeal.categoryId+'_'+#setmeal.status")
    public List<Setmeal> findByCid(Setmeal setmeal) {
        return mapper.selectList(new LambdaQueryWrapper<Setmeal>().eq(setmeal.getStatus() != null, Setmeal::getStatus, setmeal.getStatus()).eq(Setmeal::getCategoryId, setmeal.getCategoryId()));
    }
/**
  * 根据套餐id查询套餐
  * @param
  * @param
  * @return
  */
    @Override
    public Setmeal selectBySid(long sid) {
        return mapper.selectById(sid);
    }
}
